var productId;
window.onload = function (){
    //得到从前一页面提交的商品ID
    productId = getQuery("id");
    findProduct();
}

function findProduct(){
    axios.get("/post/findById",{
        params:{
            id:productId,
        }
    }).then(resp =>{
        var info = resp.data;
        $("nameSpan").innerHTML = info.postWriter.userName;
        $("dateSpan").innerHTML = info.date;
        $('replyNumSpan').innerHTML = info.replyNum;
        $("postName").innerHTML=info.postName;
        $("postDate").innerHTML=info.date;
        $("postContent").innerHTML=info.postContent;
        this.fullTable(info.replyList);
    });

    axios.get("/user/getLoginUser").then(resp=>{
        var info = resp.data;
        $("userImg").src = "/img/"+info.userImg;
    });
}


/**
 * 填充回帖表格
 * @param speakList
 */
function fullTable(replyList){
    var str = "";
    replyList.forEach(n =>{
        str += `<tr><td>${n.user.userName}</td><td>${n.replyContent}</td></tr>`;
    });
    $("data").innerHTML = str;
}

/**
 * 添加回帖
 */
function add(){
    axios.get("/reply/add",{
        params:{
            post: productId,
            replyContent:$("content").value
        }
    }).then(resp =>{
        var info = resp.data;
        if (info=="noland"){
            location.href="/login.html?errorInfo=noland";
        }
        else {
            fullTable(info);
        }

    });
}